package com.soft.springhrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soft.springhrms.mapper.EmployeesMapper;
import com.soft.springhrms.mapper.SalariesMapper;
import com.soft.springhrms.pojo.Approvesalary;
import com.soft.springhrms.pojo.Employees;
import com.soft.springhrms.pojo.Salaries;
import com.soft.springhrms.service.ApprovesalaryService;
import com.soft.springhrms.mapper.ApprovesalaryMapper;
import com.soft.springhrms.vo.ApprovesalaryVO;
import com.soft.springhrms.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author DELL
 * @description 针对表【approvesalary】的数据库操作Service实现
 * @createDate 2024-11-16 18:48:17
 */
@Service
public class ApprovesalaryServiceImpl extends ServiceImpl<ApprovesalaryMapper, Approvesalary> implements ApprovesalaryService{
    @Autowired
    private ApprovesalaryMapper approveSalaryMapper;
    @Autowired
    private EmployeesMapper employeesMapper;
    @Autowired
    private SalariesMapper salariesMapper;

    @Override
    public ResultVO getAllApproveSalary(int pageNum, int pageSize) {
        // 执行分页查询
        Page<Approvesalary> page = new Page<>(pageNum, pageSize);
        IPage<Approvesalary> resultPage = approveSalaryMapper.selectPage(page,null);
        // 判断结果是否为空
        if (resultPage.getRecords().isEmpty()) {
            return ResultVO.success(resultPage, "暂无数据");
        }
        List<ApprovesalaryVO> approveSalaryVOList = new ArrayList<>();
        for(Approvesalary approveSalary : resultPage.getRecords()) {
            ApprovesalaryVO approveSalaryVO = new ApprovesalaryVO();
            BeanUtils.copyProperties(approveSalary, approveSalaryVO);
            Integer employeeId = approveSalary.getEmployeeId();
            Employees employee = employeesMapper.selectById(employeeId);
            if (employee != null) {
                approveSalaryVO.setEmployeeName(employee.getFullName()); // 设置 employeeName
            }
            Integer hrId = approveSalary.getHrId();
            Employees hr = employeesMapper.selectById(hrId);
            if (hr != null) {
                approveSalaryVO.setHrName(employee.getFullName()); // 设置 employeeName
            }
            approveSalaryVO.setNetSalary(approveSalary.getBaseSalary().add(approveSalary.getBonusSalary()).subtract(approveSalary.getSalaryDeduction()));
            approveSalaryVOList.add(approveSalaryVO);
        }
        // 包装数据并返回
        IPage<ApprovesalaryVO> approveSalaryVOPage = new Page<>();
        approveSalaryVOPage.setRecords(approveSalaryVOList);
        approveSalaryVOPage.setTotal(resultPage.getTotal());

        // 返回成功结果
        return ResultVO.success(approveSalaryVOPage, "查询成功");
    }

    @Override
    public ResultVO ApproveSalary(Approvesalary approvesalary) {
        int rows = approveSalaryMapper.updateById(approvesalary);
        if (rows > 0) {
            if (approvesalary.getApprovalStatus().equals("通过")) {
                Salaries salaries = getSalaries(approvesalary);
                salariesMapper.insert(salaries);
                // 更新员工薪资
                QueryWrapper<Employees> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", approvesalary.getEmployeeId());
                Employees employees = employeesMapper.selectOne(queryWrapper);
                BigDecimal newBaseSalary = approvesalary.getBaseSalary();
                employees.setBaseSalary(newBaseSalary);
                employeesMapper.updateById(employees);
                return ResultVO.success("审批成功(已通过)");
            }
            return ResultVO.success("审批成功(已拒绝)");
        } else {
            return ResultVO.error(404, "未找到对应请求记录");
        }
    }

    private static Salaries getSalaries(Approvesalary approvesalary) {
        Salaries salaries = new Salaries();
        salaries.setEmployeeId(approvesalary.getEmployeeId());
        salaries.setBaseSalary(approvesalary.getBaseSalary());
        salaries.setBonusSalary(approvesalary.getBonusSalary());
        salaries.setSalaryDeduction(approvesalary.getSalaryDeduction());
        salaries.setNetSalary(approvesalary.getBaseSalary().add(approvesalary.getBonusSalary()).subtract(approvesalary.getSalaryDeduction()));
        salaries.setPaymentDate(new Date());
        return salaries;
    }

    @Override
    public ResultVO addApproveSalary(Approvesalary approvesalary) {
        int rows = approveSalaryMapper.insert(approvesalary);
        if (rows > 0) {
            return ResultVO.success("添加成功");
        } else {
            return ResultVO.error(405, "添加失败");
        }
    }

}




